package com.xbongbong.paas.help.symbol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;

import java.util.List;
import java.util.Objects;

/**
 * 文本格式进行校验
 * 单行文本、 多行文本、流水号
 * @author 吴峰
 * @date 2021/01/07 14:32
 */
public class TextSymbolHandle extends AbstractSymbolHandle {

    @Override
    public boolean equals(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return equals(target, source.getData().get(fieldAttrEntity.getAttr()));
    }

    @Override
    public boolean noEquals(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return noEquals(target, source.getData().get(fieldAttrEntity.getAttr()));
    }

    @Override
    public boolean empty(PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return empty(source.getData().get(fieldAttrEntity.getAttr()));
    }

    @Override
    public boolean noEmpty(PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return noEmpty(source.getData().get(fieldAttrEntity.getAttr()));
    }


    @Override
    public boolean like(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        boolean isNull = Objects.isNull(source) || Objects.isNull(source.getData()) || Objects.isNull(source.getData().get(fieldAttrEntity.getAttr()));
        if (Objects.isNull(target) && isNull) {
            return true;
        }
        if (isNull) {
            // 设置的值不为空，数据表中的数据为空
            return false;
        }
        return source.getData().getString(fieldAttrEntity.getAttr()).contains(target.toString());
    }

    @Override
    public boolean noLike(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.isNull(source.getData().get(fieldAttrEntity.getAttr()))) {
            return true;
        }
        return  !source.getData().getString(fieldAttrEntity.getAttr()).contains(target.toString());
    }

    @Override
    public boolean in(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return in(target, source.getData().get(fieldAttrEntity.getAttr()));
    }

    @Override
    public boolean noIn(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return noIn(target, source.getData().get(fieldAttrEntity.getAttr()));
    }

    @Override
    public boolean include(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.isNull(source.getData().get(fieldAttrEntity.getAttr()))) {
            return false;
        }
        List<Object> list = JSON.parseArray(JSON.toJSONString(target));
        for (Object item : list) {
            if (source.getData().getJSONArray(fieldAttrEntity.getAttr()).contains(item)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean allInclude(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        JSONArray dataList = source.getData().getJSONArray(fieldAttrEntity.getAttr());
        dataList = dataList == null ? new JSONArray() : dataList;
        JSONArray sourceList = JSON.parseArray(JSON.toJSONString(target));
        sourceList = sourceList == null ? new JSONArray() : sourceList;
        return Objects.equals(dataList, sourceList);
    }

    @Override
    public boolean greaterEqual(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.isNull(source.getData().get(fieldAttrEntity.getAttr()))) {
            return true;
        }
        Number a = (Number) source.getData().get(fieldAttrEntity.getAttr());
        Number b = (Number) target;
        return greaterEqual(a, b);
    }

    @Override
    public boolean lessEqual(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.isNull(source.getData().get(fieldAttrEntity.getAttr()))) {
            return false;
        }
        Number a = (Number) source.getData().get(fieldAttrEntity.getAttr());
        Number b = (Number) target;
        return lessEqual(a, b);
    }

    @Override
    public boolean greaterThan(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.isNull(source.getData().get(fieldAttrEntity.getAttr()))) {
            return true;
        }
        Number a = (Number) source.getData().get(fieldAttrEntity.getAttr());
        Number b = (Number) target;
        return greaterThan(a, b);
    }

    @Override
    public boolean lessThan(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.isNull(source.getData().get(fieldAttrEntity.getAttr()))) {
            return false;
        }
        Number a = (Number) source.getData().get(fieldAttrEntity.getAttr());
        Number b = (Number) target;
        return lessThan(a, b);
    }

    @Override
    public boolean range(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (Objects.isNull(source.getData().get(fieldAttrEntity.getAttr()))) {
            return false;
        }
        Number data = (Number) source.getData().get(fieldAttrEntity.getAttr());
        JSONArray jsonObject = JSONObject.parseArray(JSON.toJSONString(target));
        Number min = (Number) jsonObject.get(0);
        Number max = (Number) jsonObject.get(1);
        return range(min, max, data);
    }

    @Override
    public boolean dynamic(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getData().getLong(fieldAttrEntity.getAttr());
        if (Objects.isNull(dateTime)) {
            return false;
        }
        JSONArray jsonObject = JSONObject.parseArray(JSON.toJSONString(target));
        Number min = (Number) jsonObject.get(0);
        Number max = (Number) jsonObject.get(1);
        return dynamic(min, max, dateTime);
    }
}
